<?php

/**
 * Class ApiUser
 */
class ApiUser
{
    const ERROR_CODE_CLASS_EMPTY = 11;
    const ERROR_CODE_METHOD_EMPTY = 12;
    const ERROR_CODE_USERNAME_EMPTY = 13;
    const ERROR_CODE_PASSWORD_EMPTY = 14;
    const ERROR_CODE_USER_ILLEGAL = 21;
    const ERROR_CODE_USER_UNFOUND = 22;
    const ERROR_CODE_USER_NOAUTH = 23;
    const ERROR_MSG_CLASS_EMPTY = 'Class is empty';
    const ERROR_MSG_METHOD_EMPTY = 'Method is empty';
    const ERROR_MSG_USERNAME_EMPTY = 'username is empty';
    const ERROR_MSG_PASSWORD_EMPTY = 'passowrd is empty';
    const ERROR_MSG_USER_ILLEGAL = 'user illegal';
    const ERROR_MSG_USER_UNFOUND = 'user unfound';
    const ERROR_MSG_USER_NOAUTH = 'user no auth';

    public static function is_api_auth($params = array())
    {
        $res = array('code' => 0, 'msg' => '');
        $username = '';
        if (empty($params['class'])) {
            $res['code'] = self::ERROR_CODE_CLASS_EMPTY;
            $res['msg'] = self::ERROR_MSG_CLASS_EMPTY;
            return $res;
        }
        if (empty($params['method'])) {
            $res['code'] = self::ERROR_CODE_METHOD_EMPTY;
            $res['msg'] = self::ERROR_MSG_METHOD_EMPTY;
            return $res;
        }
        $user = null;
        if (empty($params['token'])) {
            if (empty($params['user'])) {
                $res['code'] = self::ERROR_CODE_USERNAME_EMPTY;
                $res['msg'] = self::ERROR_MSG_USERNAME_EMPTY;
                return $res;
            } elseif (empty($params['password'])) {
                $res['code'] = self::ERROR_CODE_PASSWORD_EMPTY;
                $res['msg'] = self::ERROR_MSG_PASSWORD_EMPTY;
                return $res;
            } else {
                $user = User::model()->findByAttributes(array('name' => $params['user']));
            }
        } else {
            $user = User::model()->findByAttributes(array('token' => $params['token']));
        }
        if (!$user) {
            $res['code'] = self::ERROR_CODE_USER_UNFOUND;
            $res['msg'] = self::ERROR_MSG_USER_UNFOUND;
            return $res;
        }
        if (empty($params['token'])) {
            if ($user->validatePassword($params['password'])) {
                $res['code'] = self::ERROR_CODE_USER_ILLEGAL;
                $res['msg'] = self::ERROR_MSG_USER_ILLEGAL;
                return $res;
            }
        }
        if (!$user->validateAuth($params['class'], $params['method'])) {
            $res['code'] = self::ERROR_CODE_USER_NOAUTH;
            $res['msg'] = self::ERROR_MSG_USER_NOAUTH;
            return $res;
        }
        return $res;
    }


    //获取用户所有的认证
    /**
     * @param string $params , sfsih
     * @return mixed
     */
    public static function get_auths($params)
    {
        $userid = isset($params['userid']) ? $params['userid'] : 0;
        $auths = User::model()->getAuths($userid);
        return $auths;
    }

    //获取用户所有的子用户
    /**
     * @param $params
     * @return array
     */
    public static function get_subusers($params)
    {
        $userid = isset($params['userid']) ? $params['userid'] : 0;
        $users = User::model()->getSubUsers($userid);
        $arUsers = dotm_ar_models2ars($users);
        return $arUsers;
    }

    /**
     * @param $params
     */
    public static function set_config($params)
    {
        $userid = isset($params['userid']) ? $params['userid'] : 0;
        $module = isset($params['module']) ? $params['module'] : '';
        $type = isset($params['type']) ? $params['type'] : '';
        $configs = isset($params['config']) ? $params['config'] : '';

        UserConfig::setConfig($userid, $module, $type, $configs);
    }

    /**
     * @param $params
     * @return mixed|null|string
     */
    public static function get_config($params)
    {
        $userid = isset($params['userid']) ? $params['userid'] : 0;
        $module = isset($params['module']) ? $params['module'] : '';
        $type = isset($params['type']) ? $params['type'] : '';

        return UserConfig::getConfig($userid, $module, $type);
    }


}